//
//  SearchView.m
//  chartTest
//
//  Created by work on 2018/11/13.
//  Copyright © 2018年 lihaiyang. All rights reserved.
//首页头部View

#import "SearchView.h"
#import "BYEdgeLabel.h"//UILabel的内边距
#import "BYHomeTopCarouselModel.h"//轮播图model

@interface SearchView()<SDCycleScrollViewDelegate>
/**扫一扫*/
@property(nonatomic,weak)UIButton *scanBtn;
/**搜索的背景*/
@property(nonatomic,weak)UIView *searchBgView;
/**城市名*/
/**分割线*/
@property(nonatomic,weak)UIView *lineView;
/**搜索按钮*/
@property(nonatomic,weak)UIButton *searchBtn;
/**温度*/
@property(nonatomic,weak)UILabel *temperatureLab;
/**天气图标*/
@property(nonatomic,weak)UIImageView *weatherImgView;
/**当时的温度*/
@property(nonatomic,strong)BYEdgeLabel *currentTemperLab;
/**天气：小雨*/
@property(nonatomic,weak)UILabel *weatherLab;
/**预警*/
@property(nonatomic,weak)UIButton *warningBtn;
/**PM10*/
@property(nonatomic,weak)UILabel *pmTenLab;
/**PM2.5*/
@property(nonatomic,weak)UILabel *pmTwoLab;
/**限行*/
//@property(nonatomic,weak)UILabel *limitRowLab;
/**轮播图*/
@property (nonatomic ,strong) SDCycleScrollView *rotationView;

@end
@implementation SearchView
#pragma mark- init初始化
- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        [self initAddSubViews];
    }
    return self;
}
- (void)initAddSubViews{
    //扫一扫
    _scanBtn = [BYVS createBtnNorIcon:@"homeIcon_scan" selIcon:@"" addSubView:self];
    [_scanBtn addTarget:self action:@selector(scanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    //搜索的背景
    _searchBgView = [BYVS createViewBGColor:RGBA(159, 200, 244, 0.66) addSubView:self];
    _searchBgView.layer.cornerRadius = BY_Height(57/2.0);
    _searchBgView.layer.masksToBounds = YES;
    //城市名
    _cityLab = [BYVS createLabText:@"丽江" font:BYPTFont28 labColor:BYColor9 addSubView:_searchBgView];
    _cityLab.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cityLabClick)];
    [_cityLab addGestureRecognizer:tap];
    [_cityLab sizeToFit];
    //分割线
    _lineView = [BYVS createViewBGColor:BYColor9 addSubView:_searchBgView];
    //搜索按钮
    _searchBtn = [BYVS createBtnImg:@"homeIcon_search" titStr:@"搜索" titColor:BYColor9 titFont:BYPTFont28 addSubView:_searchBgView];
    _searchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, BY_Width(-200), 0, BY_Width(210));
    _searchBtn.titleEdgeInsets = UIEdgeInsetsMake(0, BY_Width(-400), 0, 0);
    [_searchBtn addTarget:self action:@selector(seachBtnClick) forControlEvents:UIControlEventTouchUpInside];
    //当时的温度
    _currentTemperLab = [[BYEdgeLabel alloc]init];
    _currentTemperLab.text = @"--";
    _currentTemperLab.font = BYPTFont56;
    _currentTemperLab.textColor= BYColor9;
    _currentTemperLab.textInsets = UIEdgeInsetsMake(0.f, 0.f, -12.f, 0.f); //设置内边距
    [self addSubview:_currentTemperLab];
    [_currentTemperLab sizeToFit];
//    _currentTemperLab.backgroundColor = BYRedColor;

    //温度
    _temperatureLab = [BYVS createLabText:@"--" font:BYPTFont29 labColor:BYColor9 addSubView:self];
    [_temperatureLab sizeToFit];
    
    //天气：小雨
    _weatherLab = [BYVS createLabText:@"--" font:BYPTFont29 labColor:BYColor9 addSubView:self];
    [_weatherLab sizeToFit];
    
    //预警
    _warningBtn = [BYVS createBtnImg:@"" titStr:@"" titColor:BYColor9 titFont:BYPTFont28 addSubView:self];
    _warningBtn.backgroundColor = RGBA(0, 0, 0, 0.25);
    _warningBtn.layer.cornerRadius = BY_Width(10);
    _warningBtn.layer.masksToBounds = YES;
    
    //天气图标
    _weatherImgView = [BYVS createImgView:@"" addSubView:self];
    //PM10
    _pmTenLab =  [BYVS createLabText:@"PM10 --" font:BYPTFont16 labColor:BYColor9 addSubView:self];
    //PM2.5
    _pmTwoLab =  [BYVS createLabText:@"PM2.5 --" font:BYPTFont16 labColor:BYColor9 addSubView:self];

    //限行
//    _limitRowLab = [BYVS createLabText:@"不限行" font:BYPTFont26 labColor:BYColor9 addSubView:self];
//    _limitRowLab.hidden = YES;
//    [_limitRowLab sizeToFit];
    
    //轮播图
    _rotationView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, 0, 0) delegate:nil placeholderImage:[UIImage imageNamed:@"placeholder"]];
    _rotationView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    _rotationView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    _rotationView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    _rotationView.delegate = self;
    _rotationView.currentPageDotImage = [UIImage imageNamed:@"轮播长条"];
    _rotationView.pageDotImage = [UIImage imageNamed:@"轮播圆点"];
    _rotationView.pageControlDotSize  = CGSizeMake(20, 5);

    [self addSubview:_rotationView];
    
    [self addSubViewFrames];
}
- (void)addSubViewFrames{
    BY_kWeakSelf(ws);
    //扫一扫
    [_scanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.mas_top).offset(BY_Height(20));
        make.height.offset(BY_Height(52));
        make.width.offset(BY_Height(52));
        make.right.equalTo(ws.mas_right).offset(BY_Width(-24));
    }];
    //搜索的背景
    [_searchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.mas_left).offset(BY_Width(24));
        make.centerY.equalTo(ws.scanBtn.mas_centerY);
        make.width.offset(BY_ScreenWidth - BY_Width(36) - BY_Width(24) - BY_Height(52));
        make.height.offset(BY_Height(57));
    }];
    //城市名
    [_cityLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.searchBgView.mas_left).offset(BY_Width(15));
        make.centerY.equalTo(ws.searchBgView.mas_centerY);
    }];
    //分割线
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.cityLab.mas_right).offset(BY_Width(9));
        make.height.offset(BY_Height(30));
        make.centerY.equalTo(ws.cityLab.mas_centerY);
        make.width.offset(BY_Width(2));
    }];
    //搜索按钮
    [_searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.cityLab.mas_right).offset(BY_Width(20));
        make.centerY.equalTo(ws.searchBgView.mas_centerY);
        make.right.equalTo(ws.searchBgView.mas_right).offset(BY_Width(-9));
    }];
    //温度
    [_temperatureLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.mas_left).offset(BY_Width(39));
        make.bottom.equalTo(ws.mas_bottom).offset(-BY_Height(235+18));
    }];
    //当时的温度
    [_currentTemperLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.temperatureLab.mas_left);
        make.bottom.equalTo(ws.temperatureLab.mas_top).offset(-BY_Height(16));
    }];
     //天气：小雨
    [_weatherLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.currentTemperLab.mas_right).offset(BY_Width(14));
        make.bottom.equalTo(ws.currentTemperLab.mas_bottom);
    }];
    //预警
    [_warningBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(BY_Width(167));
        make.height.offset(BY_Width(42));
        make.centerY.equalTo(ws.weatherLab.mas_centerY);
        make.left.equalTo(ws.weatherLab.mas_right).offset(BY_Width(9));
    }];
    //天气图标
    [_weatherImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(ws.mas_right).offset(-BY_Width(130));
        make.width.height.offset(BY_Height(110));
        make.bottom.equalTo(ws.mas_bottom).offset(-BY_Height(235+24));
    }];
    //PM10
    [_pmTenLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.weatherImgView.mas_top).offset(BY_Height(10));
        make.left.equalTo(ws.weatherImgView.mas_right);
        make.right.equalTo(ws.mas_right).offset(BY_Width(-12));
    }];
    //PM2.5
    [_pmTwoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(ws.pmTenLab);
        make.top.equalTo(ws.pmTenLab.mas_bottom);
    }];
    //限行
//    [_limitRowLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(ws.mas_left).offset(BY_Width(24));
//        make.top.equalTo(ws.weatherLab.mas_bottom).offset(BY_Height(16));
//    }];
    //轮播图
    [self.rotationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(ws.mas_bottom);
        make.left.equalTo(ws.mas_left);
        make.width.offset(BY_ScreenWidth);
        make.height.offset(BY_Height(235));
    }];
}
#pragma mark- 数据处理
- (void)setTopCarDataArr:(NSArray *)topCarDataArr{//设置轮播图的数据
    _topCarDataArr = topCarDataArr;
    
    NSMutableArray *urlArrM = [NSMutableArray arrayWithCapacity:_topCarDataArr.count];
    for (BYHomeTopCarouselModel *model in _topCarDataArr) {
        [urlArrM addObject:model.img];
    }
    NSArray *imagesURLStrings = urlArrM.copy;
    _rotationView.imageURLStringsGroup = imagesURLStrings;
}
- (void)setCurrentTempStr:(NSString *)currentTempStr{//实时温度
    _currentTempStr = currentTempStr;
    _currentTemperLab.text = [NSString stringWithFormat:@"%@℃",_currentTempStr];
}
- (void)setTemperatureStr:(NSString *)temperatureStr{//温度范围：
    _temperatureStr = temperatureStr;
    self.temperatureLab.text = _temperatureStr;
}
- (void)setWeatherStr:(NSString *)weatherStr{//天气：小雨
    _weatherStr = weatherStr;
    self.weatherLab.text = _weatherStr;
}
- (void)setPMTenStr:(NSString *)tenStr pmTwoStr:(NSString *)twoStr{//pm10和pm2.5
    //PM10
    _pmTenLab.text = [NSString stringWithFormat:@"PM10  %@",tenStr];
    //PM2.5
    _pmTwoLab.text = [NSString stringWithFormat:@"PM2.5 %@",twoStr];
}
- (void)setWeatherIconStr:(NSString *)weatherIconStr{
    _weatherIconStr = weatherIconStr;
    self.weatherImgView.image = [UIImage imageNamed:_weatherIconStr];
}
- (void)setWarningStr:(NSString *)warningStr{//预警
    _warningStr = warningStr;
    if (_warningStr.length > 0) {
        [_warningBtn setImage:[UIImage imageNamed:@"home_warning"] forState:UIControlStateNormal];
        [_warningBtn setTitle:_warningStr forState:UIControlStateNormal];
        _warningBtn.hidden = NO;
    }else{
        [_warningBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [_warningBtn setTitle:@"" forState:UIControlStateNormal];
        _warningBtn.hidden = YES;
    }
}
-(void)seachBtnClick{//点击了搜索按钮
    if (self.clickSeachBtn) {
        self.clickSeachBtn();
    }
}
-(void)scanBtnClick{//点击了扫一扫按钮
    if (self.clickScanBtn) {
        self.clickScanBtn();
    }
}
-(void)cityLabClick{//点击了搜索按钮
    
    if (self.clickAddnBtn) {
        self.clickAddnBtn();
    }
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{//点击了轮播图
    if (self.clickRotationIconView) {
        self.clickRotationIconView(index);
    }
}

@end
